$(function() {
var car_num = 100;
var qidian=[];
var destination=[];
	qidian.push({$item.from_lon-0});
	qidian.push({$item.from_lat-0});
	destination.push({$item.to_lat-0});
	destination.push({$item.to_lat-0});
	$('.dispatch').on('click', function() {
		sendForm($(this));
	})
	var showlocation = {
		offset: function(othis) {
			var type = othis.data('type'),
				text = othis.text();

			layer.open({
				type: 1,
				area: ['800px', '800px'],
				title: false,
				offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
					,
				id: 'layerDemo' + type //防止重复弹出
					,
				content: '<div id="showlocation" style="width:99%;height:99%;margin:1%"></div>',
				btn: ['确认'],
				success: function(layero) {
					var btn = layero.find('.layui-layer-btn');
				},
				btn2: function() {
					layer.closeAll();
				},
				btnAlign: 'c', //按钮居中
				shade: 0, //不显示遮罩
				yes: function() {
					layer.closeAll();
				}
			});
		}
	};
	//	$('i.am-icon-map').on('click', function() {
	//		console.log(1)
	//		var othis = $(this),
	//			method = othis.data('method');
	//		showlocation[method] ? showlocation[method].call(this, othis) : '';
	//		var map = new AMap.Map('showlocation',{
	//			resizeEnable: true,
	//			zoom: 11,
	//			center: [118.57, 24.82]
	//		});
	//	})
	var origin = [];
	var map = new AMap.Map('carmap', {
		resizeEnable: true,
		zoom: 11,
		center: [118.57, 24.82]
	});

	//构造路线导航类
	var driving = new AMap.Driving({
		map: map,
		//		panel: "panel"
	});
	 var infoWindow;
	AMapUI.loadUI(['overlay/SimpleMarker','overlay/SimpleInfoWindow'], function(SimpleMarker, SimpleInfoWindow) {
		var dispatchload = layer.load(0, {
			shade:  [0.3,'#fff']
		});
		var iconStyles = SimpleMarker.getBuiltInIconStyles('fresh');
		$.ajax({
			type: "post",
			url: "{:url('/admin/beidou/cars')}",
			data: {
				pagesize: car_num
				{if $Think.get.number},number:{$Think.get.number}{/if}
			},
			success: function(res) {
				layer.close(dispatchload);
				if(res.code == 1) {
					if(typeof res == 'string') {
					origin.car = JSON.parse(res).data;
				} else {
					origin.car = res.data;
				};
				console.log(origin)
				for(var caridx = 0; caridx < origin.car.length; caridx++) {
					if(origin.car[caridx].work_state==3){
						addcarMarker(origin, caridx, SimpleMarker)
					}
				}
				for(var j = 0; j < origin.car.length; j++) {
					var index = j
					origin.car[j].point.on('click', function() {
						openmessage(map, this, SimpleInfoWindow, infoWindow)
						console.log(this)
						for(var index in this) {
							for(var index1 in this[index]) {
								if(index1 == "cont") {
									console.log(this[index].cont.num)
									$('.driver').html(this[index].cont.name)
									$('.driver').next('input').val(this[index].cont.name)
									$('.driver_phone').html(this[index].cont.phone)
									$('.driver_phone').next('input').val(this[index].cont.phone)
									$('.number').html(this[index].cont.num)
									$('.number').next('input').val(this[index].cont.num)
									$('.car_id').val(this[index].cont.carid)
								};
							};
						};
		
					})
				}
				}
			}
		})
		function addcarMarker(origin, caridx, SimpleMarker) {
		var carimg;
		if(origin.car[caridx].work_state==3) {
			pointimg='{$static_dir}/assets/img/car.png'
		}else if(origin.car[caridx].work_state==4){
			pointimg='{$static_dir}/assets/img/car.png'
		}else{
			pointimg='{$static_dir}/assets/img/car.png'
		};
		origin.car[caridx].point = new SimpleMarker({
			//普通文本
			iconStyle: {
				src: pointimg,
				style: {
					width: '15px',
					height: '30px'
				}
			},
			position: origin.car[caridx].loc,
			cont: {
				carid: origin.car[caridx].car_id,
				driverid: origin.car[caridx].driver_id,
				num: origin.car[caridx].number,
				loc: origin.car[caridx].loc,
				name: origin.car[caridx].name,
				phone: origin.car[caridx].phone,
				work_state: origin.car[caridx].work_state
			}
		});
		origin.car[caridx].point.setMap(map);
	}
		
		

	});
	function openmessage(map, obj, SimpleInfoWindow, infoWindow) {
	var arr, idx, content, pos,state;
	for(var index in obj) {
		for(var index1 in obj[index]) {
			if(index1 == "cont") {
				idx = index;
				arr = obj[index].path;
				pos = obj[index].cont.loc;
				content = obj[index].cont;
			};
		};
	};
	
	
	
		if(content.work_state==1){
		state='执行任务中'
	}else if(content.work_state==2){
		state='有预约订单'
	}else if(content.work_state==3){
		state='听单中'
	}else{
		state='下班'
	}
		infoWindow = new SimpleInfoWindow({
			infoTitle: '<strong>' + content.name + '('+state+')</strong>',
			infoBody: `<div><div">电话：${content.phone}</div><div>车牌号:${content.num}</div></div>`,
			offset: new AMap.Pixel(0, -31)
		});
	
	infoWindow.open(map, obj.getPosition());
}
})